package com.shitou.web;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.shitou.pojo.Document;
import com.shitou.pojo.Project;
import com.shitou.pojo.User;
import com.shitou.service.DocumentService;
import com.shitou.service.ProjectService;

@Results({@Result(name=BaseAction.PROJECT,location="project.jspx",params={"id","${project.id}"},type="redirectAction"),
	@Result(name=BaseAction.RELOAD,location="document.jspx",params={"id","${id}"},type="redirectAction")})
public class DocumentAction extends BaseAction{

	private static final long serialVersionUID = 1L;

	@Resource
	private DocumentService documentService;
	@Resource
	private ProjectService projectService;
	private Integer id;
	private Document document;
	private Project project;
	private Integer projectid;
	
	@Override
	public String del() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		project = projectService.findById(projectid);
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		document = documentService.findById(id);
		return SUCCESS;
	}

	@Override
	public String save() throws Exception {
		document.setProject(project);
		Document documentTmp = documentService.findById(document.getId());
		if(documentTmp == null) {
			id = documentService.save(document,(User)getSessionAttribute("user"));
		} else {
			documentTmp.setTitle(document.getTitle());
			documentTmp.setContent(document.getContent());
			id = documentService.edit(documentTmp,(User)getSessionAttribute("user"));
		}
		return RELOAD;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getProjectid() {
		return projectid;
	}

	public void setProjectid(Integer projectid) {
		this.projectid = projectid;
	}
}
